<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <el-input
        v-model="query['member.memberName']"
        clearable
        placeholder="會員名稱"
        style="width: 185px;"
        class="filter-item"
        @keyup.enter.native="crud.toQuery"
      />
      <el-input
        v-model="query['member.phone']"
        clearable
        placeholder="聯繫電話"
        style="width: 185px;"
        class="filter-item"
        @keyup.enter.native="crud.toQuery"
      />
      <rrOperation :crud="crud" />
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation>
        <template #right>
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            @click="openDialog(2,'錢包')"
          >
            調整錢包
          </el-button>
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            @click="openDialog(0, '積分')"
          >
            調整積分
          </el-button>
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            @click="openDialog(1,'成長值')"
          >
            調整成長值
          </el-button>
        </template>
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        stripe
        @selection-change="crud.selectionChangeHandler"
        @sort-change="sortChange"
      >
        <el-table-column prop="member.memberName" label="會員名稱" align="right" sortable />
        <el-table-column prop="member.phone" label="手機號" align="right" sortable />
        <el-table-column prop="balance" label="可用余额" align="right" sortable />
        <el-table-column prop="growth" label="可用成长值" align="right" sortable />
        <el-table-column prop="score" label="可用积分" align="right" sortable />
        <el-table-column prop="totalBalance" label="总余额" align="right" sortable />
        <el-table-column prop="totalGrowth" label="总成长值" align="right" sortable />
        <el-table-column prop="totalScore" label="总积分" align="right" sortable />
      </el-table>
      <!--分页组件-->
      <pagination />
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
      >
        <el-col :span="24">
          <span>選擇用戶:</span>
          <el-select
            v-model="memberId"
            filterable
            remote
            placeholder="请选择"
            :remote-method="getUserMethod"
            :loading="loading"
          >
            <el-option
              v-for="item in user"
              :key="item.id"
              :label="item.memberName"
              :value="item.id"
            />
          </el-select>
          <span style="color: #FFBA00">用戶姓名和手機號任選其一即可</span>
        </el-col>
        <el-col :span="24" style="margin-top: 20px">
          <span>選擇號碼:</span>
          <el-select v-model="memberId" filterable placeholder="请选择">
            <el-option
              v-for="item in user"
              :key="item.id"
              :label="item.phone"
              :value="item.id"
            />
          </el-select>
          <span style="color: #FFBA00">用戶姓名和手機號任選其一即可</span>
        </el-col>
        <el-col :span="24" style="margin-top: 20px">
          <span> 調整{{ typeText }}:</span>
          <el-input-number v-model="AdjustNumber" style="width: 200px" />
          <span style="color: #FFBA00">例:填10給用戶增加10對應資產,-10給用戶減少10對應資產</span>
        </el-col>
        <el-col :span="24" style="margin-top: 20px;margin-bottom: 20px">
          <span> 操作備註:</span>
          <el-input
            v-model="operateNote"
            type="textarea"
            :rows="3"
            style="width: 400px"
          />
        </el-col>
        <span slot="footer" class="dialog-footer" style="margin-top: 20px">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="completeProduction">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudMemberAsset from '@/api/ums/memberAsset'
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import { page } from '@/api/ums/member'

export default {
  name: 'MemberAsset',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), crud()],
  cruds() {
    return CRUD({
      title: '用户资产',
      url: 'memberAsset/page',
      sort: 'id:desc',
      crudMethod: { ...crudMemberAsset },
      optShow: {}
    })
  },
  data() {
    return {
      type: '',
      typeText: '',
      dialogVisible: false,
      user: [],
      userPhone: [],
      AdjustNumber: '',
      memberId: '',
      operateNote: '',
      loading: false
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    openDialog(type, typeText) {
      this.type = type
      this.typeText = typeText
      this.dialogVisible = true
    },
    getUserMethod(query) {
      if (query !== '') {
        this.loading = true
        page({ memberName: query, size: 50 }).then(res => {
          if (res.code === 0) {
            this.loading = false
            this.user = res.data.rows
          }
        })
      } else {
        this.user = []
      }
    },
    completeProduction() {
      crudMemberAsset.update({
        memberId: this.memberId,
        growthType: this.type,
        changeCount: this.AdjustNumber,
        operateNote: this.operateNote
      }).then(res => {
        this.$notify({
          title: res.code === 0 ? '成功' : '失敗',
          message: res.message,
          type: res.code === 0 ? 'success' : 'error'
        })
        if (res.code === 0) {
          this.crud.refresh()
          this.dialogVisible = false
          this.memberId = null
          this.type = null
          this.AdjustNumber = null
          this.operateNote = null
        }
      })
    },
    sortChange(column) {
      const sortColumn = column.prop
      if (column.order === 'ascending') {
        this.crud.sort = [sortColumn + ':asc']
      }
      if (column.order === 'descending') {
        this.crud.sort = [sortColumn + ':desc']
      }
      this.crud.refresh()
    }
  }
}
</script>

<style scoped>

</style>
